home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 15.7 KB | 598 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: SLResFil.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWRESFI_K
- #include "FWResFil.k"
- #endif
-
- #ifndef FWRESFI_H
- #include "FWResFil.h"
- #endif
-
- #ifndef FWSTRS_H
- #include "FWStrs.h"
- #endif
-
- #ifndef FWFILESP_H
- #include "FWFileSp.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef FWBNDSTR_H
- #include "FWBndStr.h"
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // Mac-only
- //----------------------------------------------------------------------------------------
- #ifdef FW_BUILD_MAC
-
- #pragma segment FWResource_PrivateResourceFile
-
- #ifndef __ERRORS__
- #include <errors.h>
- #endif
-
- #ifndef __ICONS__
- #include <Icons.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __STRING__
- #include <string.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #endif
-
-
- /*
- * This file was generated by the SOM Compiler.
- * Generated using:
- * SOM Emitter emitxtm.dll: 2.33
- */
-
- #define VARIABLE_MACROS
- #define FW_OResourceFile_Class_Source
- #include "SLResFil.xih"
-
-
- //----------------------------------------------------------------------------------------
- // throwExceptionIfResourceLoadError
- //----------------------------------------------------------------------------------------
-
- static void throwExceptionIfResourceLoadError(FW_OResourceFile* somSelf,
- Environment* ev,
- FW_PlatformHandle resourceHandle,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType)
- {
- FW_UNUSED(somSelf);
- FW_UNUSED(ev);
-
- if (resourceHandle != NULL)
- return;
-
- // If we make it to here, we must exit with an exception.
-
- #ifdef FW_BUILD_MAC
- FW_UNUSED(resourceID);
- FW_UNUSED(resourceType);
- FW_FailOnError(::ResError());
- #endif
-
- #ifdef FW_BUILD_WIN
- if (!somSelf->HasResource(ev, resourceID, resourceType))
- FW_Failure(FW_xResourceNotFound);
- #endif
-
- // If we somehow made it to here, we force an exception
- FW_Failure(FW_xResourceNotLoaded);
- }
-
-
- //----------------------------------------------------------------------------------------
- // newFileSpecificationForID
- //----------------------------------------------------------------------------------------
-
- static FW_OFileSpecification* newFileSpecificationForID(Environment* ev,
- FW_ResourceFileID resFileID)
- {
- FW_OFileSpecification* fileSpec = new FW_OFileSpecification;
-
- #ifdef FW_BUILD_WIN
- // Get file name from Windows
- FW_Char buffer[255];
- ::GetModuleFileName(resFileID, buffer, sizeof buffer);
- FW_CString255 fileName(buffer);
-
- fileSpec->InitFromFileName(ev, fileName);
- #endif
-
- #ifdef FW_BUILD_MAC
- FSSpec macFileSpec;
- FInfo fileInfo;
- FW_PascalChar buffer[255];
- FCBPBRec pb;
-
- pb.ioFCBIndx = 0;
- pb.ioVRefNum = 0;
- pb.ioRefNum = resFileID;
- pb.ioNamePtr = buffer;
- OSErr err = ::PBGetFCBInfoSync(&pb);
- if (err != noErr)
- {
- buffer[0] = 0;
- pb.ioFCBVRefNum = 0;
- pb.ioFCBParID = 0;
- }
-
- ::FSMakeFSSpec(pb.ioFCBVRefNum, pb.ioFCBParID, buffer, &macFileSpec);
- ::FSpGetFInfo(&macFileSpec, &fileInfo);
-
- fileSpec->AssignFileSpec(ev, &macFileSpec);
- fileSpec->MacSetTypeAndCreator(ev, fileInfo.fdType, fileInfo.fdCreator);
- #endif
-
- return fileSpec;
- }
-
-
- //----------------------------------------------------------------------------------------
- // openResourceFile
- //----------------------------------------------------------------------------------------
-
- static FW_ResourceFileID openResourceFile(Environment* ev, FW_OFileSpecification* fileSpec)
- {
- #ifdef FW_BUILD_WIN
- FW_CString fileName;
-
- fileSpec->GetFullPath(ev, fileName);
-
- char szFileName[_MAX_PATH];
- fileName.ExportCString(szFileName);
- FW_ResourceFileID result = ::LoadLibrary(szFileName);
-
- #ifdef FW_BUILD_WIN16
- if (result < HINSTANCE_ERROR)
- {
- int error = result;
- #endif
- #ifdef FW_BUILD_WIN32
- if(result == NULL)
- {
- DWORD error = ::GetLastError();
- #endif
- switch (error)
- {
- case 2: // File not found.
- case 3: // Path not found.
- FW_Failure(FW_xResourceFileNotFound);
- case 8: // Insufficient memory
- FW_Failure(FW_xMemoryExhausted);
- default:
- FW_Failure(FW_xResourceFileNotFound);
- }
- #ifdef FW_BUILD_WIN32
- }
- #endif
- #ifdef FW_BUILD_WIN16
- }
- #endif
- #endif
-
- #ifdef FW_BUILD_MAC
- FSSpec macSpec;
-
- fileSpec->MacGetFSSpec(ev, &macSpec);
- FW_ResourceFileID result = ::FSpOpenResFile(&macSpec, fsRdPerm);
- if (result == -1)
- {
- short resError = ::ResError();
- if (resError == noErr)
- resError = resFNotFound;
- FW_Failure(resError);
- }
- #endif
-
- return result;
- }
-
-
- //----------------------------------------------------------------------------------------
- // closeResourceFile
- //----------------------------------------------------------------------------------------
-
- static void closeResourceFile(FW_ResourceFileID file)
- {
- #if defined FW_BUILD_MAC
- ::CloseResFile(file);
- #elif defined FW_BUILD_WIN
- ::FreeLibrary(file);
- #endif
- }
-
-
- //----------------------------------------------------------------------------------------
- // privGetResource
- //
- // Private method. Attempts to load the resource, but will return NULL on failure.
- //----------------------------------------------------------------------------------------
-
- static FW_ResourceHandle privGetResource(FW_OResourceFile *somSelf,
- Environment* ev,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType)
- {
- FW_UNUSED(ev);
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
- FW_ResourceHandle resourceHandle = NULL;
-
- #if defined FW_BUILD_WIN
- resourceHandle = ::FindResource(_fResourceFileID,
- MAKEINTRESOURCE(resourceID),
- MAKEINTRESOURCE(resourceType));
-
- if (resourceHandle == 0)
- {
- char fileName[255] = {0};
- ::GetModuleFileName(_fResourceFileID, fileName, sizeof fileName);
- __asm int 3
- }
-
- #elif defined FW_BUILD_MAC
- // Load the resource.
- short temp = CurResFile();
- if (temp != _fResourceFileID)
- ::UseResFile(_fResourceFileID);
- resourceHandle = ::Get1Resource(resourceType, short(resourceID)); // Must cast!
- if (temp != _fResourceFileID)
- ::UseResFile(temp);
- #endif
-
- return resourceHandle;
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__InitFromFileSpec
- //----------------------------------------------------------------------------------------
- /*
- * Open a resources file by name.
- * This instance assumes responsibility for closing the file.
- */
-
- SOM_Scope void SOMLINK FW_OResourceFile__InitFromFileSpec(FW_OResourceFile *somSelf, Environment *ev,
- FW_OFileSpecification* newFileSpec)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- FW_SOM_TRY
- {
- _fMustCloseFile = FALSE;
- _fResourceFileID = openResourceFile(ev, newFileSpec);
- _fMustCloseFile = TRUE;
- _fFileSpec = new FW_OFileSpecification;
- _fFileSpec->AssignOFileSpecification(ev, newFileSpec);
- }
- FW_SOM_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__InitFromResFileID
- //----------------------------------------------------------------------------------------
- /*
- * Attach to a previously opened file.
- * This instance does not assume responsibility for closing the file.
- */
-
- SOM_Scope void SOMLINK FW_OResourceFile__InitFromResFileID(FW_OResourceFile *somSelf, Environment *ev,
- FW_ResourceFileID resFileID)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- FW_SOM_TRY
- {
- _fMustCloseFile = FALSE;
- _fResourceFileID = resFileID;
- _fFileSpec = newFileSpecificationForID(ev, resFileID);
- }
- FW_SOM_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__GetFileSpecification
- //----------------------------------------------------------------------------------------
- /*
- * Get the file specification for the resourcesFile.
- */
-
- SOM_Scope FW_OFileSpecification* SOMLINK FW_OResourceFile__GetFileSpecification(FW_OResourceFile *somSelf, Environment *ev)
- {
- FW_UNUSED(ev);
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- return _fFileSpec;
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__HasResource
- //----------------------------------------------------------------------------------------
- /*
- * Returns TRUE if the resource exists in the file, FALSE if it doesn't.
- */
-
- SOM_Scope FW_Boolean SOMLINK FW_OResourceFile__HasResource(FW_OResourceFile *somSelf, Environment *ev,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- FW_SOM_TRY
- {
- #ifdef FW_BUILD_WIN
- FW_ResourceHandle resourceHandle;
- resourceHandle = ::FindResource(somThis->fResourceFileID,
- MAKEINTRESOURCE(resourceID),
- MAKEINTRESOURCE(resourceType));
- #endif
-
- #ifdef FW_BUILD_MAC
- FW_CMacResLoadFalse dontLoadResource;
- FW_PlatformHandle resourceHandle = privGetResource(somSelf, ev, resourceID, resourceType);
- #endif
-
- return (resourceHandle != NULL);
- }
- FW_SOM_CATCH
- return FALSE;
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__GetResourceHandle
- //----------------------------------------------------------------------------------------
- /*
- * Gets the resource handle. Resource data may still be purgeable or unloaded.
- * Client assumes responsibility to call ReleaseResourceHandle when done.
- */
-
- SOM_Scope FW_ResourceHandle SOMLINK FW_OResourceFile__GetResourceHandle(FW_OResourceFile *somSelf, Environment *ev,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
- FW_ResourceHandle handle = 0;
-
- FW_SOM_TRY
- {
- handle = privGetResource(somSelf, ev, resourceID, resourceType);
-
- // Throw an exception if the resource was not loaded.
- throwExceptionIfResourceLoadError(somSelf, ev, handle, resourceID, resourceType);
- }
- FW_SOM_CATCH
-
- return handle;
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__ReleaseResourceHandle
- //----------------------------------------------------------------------------------------
- /*
- * Releases the resource handle. All memory is released.
- */
-
- SOM_Scope void SOMLINK FW_OResourceFile__ReleaseResourceHandle(FW_OResourceFile *somSelf, Environment *ev,
- FW_ResourceHandle handle)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- FW_SOM_TRY
- {
- #ifdef FW_BUILD_MAC
- ::ReleaseResource(handle);
- #else
- FW_UNUSED(handle);
- #endif
- }
- FW_SOM_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__PrivHasSpecialResource
- //----------------------------------------------------------------------------------------
- /*
- * Returns TRUE if the resource exists in the file, FALSE if it doesn't.
- */
-
- SOM_Scope FW_Boolean SOMLINK FW_OResourceFile__PrivHasSpecialResource(FW_OResourceFile *somSelf, Environment *ev,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- // (JEL) I can't find any reason why this function should be different from
- // HasResource. For now, I'll just call HasResource, but maybe later I'll
- // have to do something else.
- return somSelf->HasResource(ev, resourceID, resourceType);
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__PrivGetSpecialResource
- //----------------------------------------------------------------------------------------
- /*
- * Gets the special resource handle.
- * It is the clients reponsibility to release the handle if necessary,
- * using whatever platform specific code is required.
- */
-
- SOM_Scope FW_PlatformHandle SOMLINK FW_OResourceFile__PrivGetSpecialResource(FW_OResourceFile *somSelf, Environment *ev,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
- FW_PlatformHandle handle = NULL;
-
- FW_SOM_TRY
- {
- #if defined FW_BUILD_MAC
- // (JEL) ??? Some resource types are truly "special", in that calling GetResource
- // to load them is a bad idea. However, several types of resoures have their
- // own GetXxxx toolbox call that is supposedly equivalent to just calling
- // GetResource(XXXX, id). For some of these types, we call the GetXxxx function,
- // but we allow the default handler to get the rest of these types. It bothers
- // me a little that we're not consistent, but it seems like there is no good
- // reason to worry about it.
- switch (resourceType)
- {
- case FW_kCursor:
- handle = (FW_PlatformHandle) ::GetCCursor(resourceID);
- break;
-
- case FW_kIcon:
- handle = (FW_PlatformHandle) ::GetCIcon(resourceID);
- break;
-
- case FW_kBlackWhiteIcon:
- handle = ::GetIcon(resourceID);
- break;
-
- case FW_kBlackWhiteCursor:
- handle = (FW_PlatformHandle) ::GetCursor(resourceID);
- break;
-
- case FW_kPicture:
- handle = (FW_PlatformHandle) ::GetPicture(resourceID);
- break;
-
- case FW_kBitmap:
- handle = ::GetResource(resourceType, resourceID);
- break;
-
- default:
- FW_DEBUG_MESSAGE("Not a special resource type");
- }
- #elif defined FW_BUILD_WIN
- switch(resourceType)
- {
- case FW_kCursor:
- handle = ::LoadCursor(_fResourceFileID, MAKEINTRESOURCE(resourceID));
- break;
-
- case FW_kIcon:
- handle = ::LoadIcon(_fResourceFileID, MAKEINTRESOURCE(resourceID));
- break;
-
- case FW_kBitmap:
- handle = ::LoadBitmap(_fResourceFileID, MAKEINTRESOURCE(resourceID));
- break;
-
- case FW_kAccelerator:
- handle = ::LoadAccelerators(_fResourceFileID, MAKEINTRESOURCE(resourceID));
- break;
-
- default:
- FW_DEBUG_MESSAGE("Not a special resource type");
- }
- #endif
-
- throwExceptionIfResourceLoadError(somSelf, ev, handle, resourceID, resourceType);
- }
- FW_SOM_CATCH
-
- return handle;
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__PrivGetResourceFileID
- //----------------------------------------------------------------------------------------
- /*
- * Return the platforms's "file ID" for this resources file.
- */
-
- SOM_Scope FW_ResourceFileID SOMLINK FW_OResourceFile__PrivGetResourceFileID(FW_OResourceFile *somSelf, Environment *ev)
- {
- FW_UNUSED(ev);
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- return _fResourceFileID;
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__somInit
- //----------------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK FW_OResourceFile__somInit(FW_OResourceFile *somSelf)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- FW_OResourceFile_parent_FW_ORefCount_somInit(somSelf);
-
- _fMustCloseFile = FALSE;
- _fResourceFileID = 0;
- _fFileSpec = 0;
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_OResourceFile__somUninit
- //----------------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK FW_OResourceFile__somUninit(FW_OResourceFile *somSelf)
- {
- FW_OResourceFileData *somThis = FW_OResourceFileGetData(somSelf);
-
- FW_SOM_UNINIT_TRY
- {
- if (_fMustCloseFile)
- closeResourceFile(_fResourceFileID);
- delete _fFileSpec;
-
- FW_OResourceFile_parent_FW_ORefCount_somUninit(somSelf);
- }
- FW_SOM_UNINIT_CATCH
- }
-